package com.example.lets;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.baidu.mapapi.SDKInitializer;
import com.bmob.BmobConfiguration;
import com.bmob.BmobPro;
import com.gc.materialdesign.views.ButtonRectangle;
import com.gc.materialdesign.widgets.SnackBar;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;

public class MainActivity extends Activity {

	/*
        TODO : 优化评论列表转向个人中心的参数传递

    */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_main);

        Bmob.initialize(this, "13f0e49d5141ecd8ba0aa06f484e7e34");

        User curUser = BmobUser.getCurrentUser(this, User.class);
        if (curUser != null) {

            if (curUser.getAge() == null || curUser.getGender() == null ||
                    curUser.getNickname() == null) {
                new SnackBar(MainActivity.this, "请完善您的个人信息！", null, null).show();
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, CompleteInfoActivity.class);
                startActivity(intent);
                MainActivity.this.finish();
            } else {
                BmobConfiguration config = new BmobConfiguration.Builder(this)
                        .customExternalCacheDir(curUser.getUsername()).build();
                BmobPro.getInstance(this).initConfig(config);
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, HomeActivity.class);
                startActivity(intent);
                MainActivity.this.finish();
            }
        }

        ButtonRectangle login = (ButtonRectangle) findViewById(R.id.main_login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String phone = ((EditText) MainActivity.this.findViewById(R.id.main_username_text))
                        .getText().toString();
                String pswd = ((EditText) MainActivity.this.findViewById(R.id.main_password_text))
                        .getText().toString();

                if (phone.equals("")) {
                    new SnackBar(MainActivity.this, "用户名不能为空！", null, null).show();
                    return;
                }
                if (pswd.equals("")) {
                    new SnackBar(MainActivity.this, "密码不能为空！", null, null).show();
                    return;
                }

                loginByPhonePwd(phone, pswd);
            }
        });
        login.setBackgroundColor(Color.parseColor("#E51C23"));
        login.setRippleSpeed(50);

        ButtonRectangle register = (ButtonRectangle) findViewById(R.id.main_register);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
        register.setBackgroundColor(Color.WHITE);
        register.setRippleSpeed(50);

    }

    private void loginByPhonePwd(String mobliePhoneNumber, String password) {
        BmobUser.loginByAccount(this, mobliePhoneNumber, password, new LogInListener<User>() {

            @Override
            public void done(User user, BmobException e) {
                // TODO Auto-generated method stub
                if (user != null) {
                    Log.i("Bmob", "用户登陆成功");
                    new SnackBar(MainActivity.this, "登录成功！", null, null).show();

                    // 初始化该用户的缓存路径
                    BmobConfiguration config = new BmobConfiguration.Builder(MainActivity.this)
                            .customExternalCacheDir(user.getUsername()).build();
                    BmobPro.getInstance(MainActivity.this).initConfig(config);

                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this, HomeActivity.class);
                    startActivity(intent);
                    MainActivity.this.finish();
                } else {
                    Log.i("Bmob", "用户登陆失败");
                    new SnackBar(MainActivity.this, "登录失败！", null, null).show();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
